package org.nanotek.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.inject.Named;

import org.nanotek.base.person.Person;
import org.nanotek.service.PersonService;
import org.nanotek.web.bean.PersonModel;
import org.nanotek.web.builder.PersonBuilder;

@Model
@Named(value="personController")
@SuppressWarnings("serial")
public class PersonController extends PersonModel{

	@Inject PersonService personService;

	private List<PersonModel> personModelList; 
	
	public String checkPersonService()
	{ 
		return personService !=null ? "PersonServiceActive" : "PersonServiceInactive";
	}
	
	public String addPerson ()
	{ 
		if (personService !=null) 
			personService.addPerson(new PersonBuilder().setFirstName(firstName).setLastName(lastName).setDateBirth(dateBirth).build());

		return "addPersonSuccess";
	}

	public String loadPersonModelList()
	{ 
		personModelList = new ArrayList<PersonModel>();
		Stream<PersonModel> personModelStream = personService.getPersonList().stream().
				map(
				new Function <Person , PersonModel>()
				{
					public PersonModel apply(Person t) {
						return new PersonModel(t);
					} 
				});
		
		personModelStream.forEach(
				new Consumer<PersonModel>()
					{
						@Override
						public void accept(PersonModel t) {
							personModelList.add(t);
						} 
							
					}
				);
		
		return "loadPersonModelListSuccess";
	}

	public List<PersonModel> getPersonModelList() {
		return personModelList;
	}

	public void setPersonModelList(List<PersonModel> personModelList) {
		this.personModelList = personModelList;
	}
	
}
